package com.crud.web.controller;

import com.crud.domain.ResultVo;
import com.crud.entity.Dept;
import com.crud.service.DeptService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hyh
 * @date 2023/06/28 18:46:44
 */
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @PostMapping("dept")
    public ResultVo addDept(@RequestBody Dept dept) {
        int row = deptService.addDept(dept);
        return row > 0 ? ResultVo.success("add ok~") : ResultVo.failure("false");
    }

    @DeleteMapping("dept/{id}")
    public ResultVo deleteDept(@PathVariable Integer id) {
        Dept dept = deptService.selectByOne(id);
        if (dept.getChildren() == null) {
            int row = deptService.deleteDept(id);
            return row > 0 ? ResultVo.success("delete ok~~") : ResultVo.failure("false ~~");
        }
        return ResultVo.success("有子类，删除不了",111);
    }

    @GetMapping("dept/{pageNum}/{pageSize}")
    public ResultVo findAll(@PathVariable Integer pageNum,@PathVariable Integer pageSize){
        List<Dept> list = deptService.findSame(pageNum, pageSize);
        PageInfo<Dept> pageInfo = new PageInfo<>(list);
        return ResultVo.success(pageInfo);
    }

    @PutMapping("dept")
    public ResultVo update(@RequestBody Dept dept) {
        int rows = deptService.update(dept);
        return rows > 0 ? ResultVo.success("修改成功") : ResultVo.failure("修改失败");
    }
}
